﻿namespace Test
{
    using System;
    using System.Windows.Forms;
    using MA.GoogleInc;
    using System.ComponentModel;
    using MA.Utility;
    public partial class frmGoogle : Form
    {
        Analytic analytic;
        PropAnalytic propAnalytic = new PropAnalytic();
        CredentialsAnalytic credentialsAnalytic = new CredentialsAnalytic();

        BackgroundWorker bgw = new BackgroundWorker();

        private void bgw_DoWork(object sender, DoWorkEventArgs e)
        {

            for (int i = 1; i <= 15; i++)
            {

                bgw.ReportProgress((100 * i) / 15);

                if (!string.IsNullOrEmpty(txtContent.Text))
                {
                    bgw.ReportProgress(100);

                    break;
                }
                else
                {
                    break;
                }

            }
        }

        private void bgw_ProgressChanged(object sender, ProgressChangedEventArgs e)
        {
            progressBar1.Value = Math.Min(e.ProgressPercentage, 50);
            label4.Text = String.Format("Progress: {0} %", e.ProgressPercentage);
        }

        private void bgw_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
        {
            //bgw.Dispose();
            //progressBar1.Value = 50;
        }


        public frmGoogle()
        {
            try
            {
                propAnalytic.Dimensions = "ga:latitude,ga:longitude,ga:country,ga:city,ga:metro,ga:region,ga:continent";
                propAnalytic.Metrics = "ga:visitors,ga:visits,ga:newVisits";
                propAnalytic.Segment = "";
                propAnalytic.Filters = "";
                propAnalytic.Sort = "";
                propAnalytic.StartDate = "2005-01-01";
                propAnalytic.EndtDate = DateTime.Now.Date.ToString("yyyy-MM-dd").ToString();

                bgw.DoWork += new DoWorkEventHandler(bgw_DoWork);
                bgw.ProgressChanged += new ProgressChangedEventHandler(bgw_ProgressChanged);
                bgw.RunWorkerCompleted += new RunWorkerCompletedEventHandler(bgw_RunWorkerCompleted);
                bgw.WorkerReportsProgress = true;
                bgw.WorkerSupportsCancellation = true;

                InitializeComponent();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void btoGetVisitor_Click(object sender, EventArgs e)
        {

            try
            {
                progressBar1.Value = 1;
                bgw.RunWorkerAsync();

                credentialsAnalytic.User = txtUser.Text;
                credentialsAnalytic.Pass = txtPass.Text;
                credentialsAnalytic.IDTable = txtIDTable.Text;

                analytic = new Analytic(credentialsAnalytic, propAnalytic);

                TextHelp.SetText(this, txtContent, analytic.GetVisitors());

            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void btoGetVisits_Click(object sender, EventArgs e)
        {
            try
            {
                progressBar1.Value = 1;
                bgw.RunWorkerAsync();

                credentialsAnalytic.User = txtUser.Text;
                credentialsAnalytic.Pass = txtPass.Text;
                credentialsAnalytic.IDTable = txtIDTable.Text;
                analytic = new Analytic(credentialsAnalytic, propAnalytic);
                TextHelp.SetText(this, txtContent, analytic.GetVisits());
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void btoGetNewVisits_Click(object sender, EventArgs e)
        {
            try
            {
                progressBar1.Value = 1;
                bgw.RunWorkerAsync();

                credentialsAnalytic.User = txtUser.Text;
                credentialsAnalytic.Pass = txtPass.Text;
                credentialsAnalytic.IDTable = txtIDTable.Text;
                analytic = new Analytic(credentialsAnalytic, propAnalytic);
                TextHelp.SetText(this,txtContent, analytic.GetNewVisits());
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void btoGetLatLogResultJsonString_Click(object sender, EventArgs e)
        {
            try
            {
                progressBar1.Value = 1;
                bgw.RunWorkerAsync();

                credentialsAnalytic.User = txtUser.Text;
                credentialsAnalytic.Pass = txtPass.Text;
                credentialsAnalytic.IDTable = txtIDTable.Text;
                analytic = new Analytic(credentialsAnalytic, propAnalytic);
                TextHelp.SetText(this, txtContent, analytic.GetLatLogResultJsonString());
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

    }
}
